<?php


class TemplateController extends ControllerBase
{
    public $site = 'http://www.drinkt.org.cn/';
    public function listAction(){
        $pageNum = $this->request->getQuery("pageNum", null, 1);
        $size = 50;
        $limit = ($pageNum - 1) * 50;
        $name = $this->request->get("name");
        $where = "";
        if ($name) {
            $where .= " name like '{%$name%}'";
        }

        $list = Template::find([
            "conditions" => $where,
            "limit" => $size,
            "order" => "create_at desc ",
            "offset" => $limit
        ]);

        $total = Template::count($where);
        $allPageNum = ceil($total / $size);
        $data= [];
        foreach ($list as $key=>$vo){
            $data[$key]=$vo->toArray();
            $data[$key]['path']=$this->site.$vo->path;
        }
        $this->exitMessage(true, '', ["data" => $data, "pageInfo" => ["size" => $size, "current" => $pageNum, "allPageNum" => $allPageNum, "total" => $total]]);

    }
    
    public function saveAction(){
        $title = $this->request->get("title");
        $content = $this->request->get("content");
        $is_html = $this->request->get("is_html");
        $headBar = $this->request->get("headBar");
        $buyList = $this->request->get("buyList");
        $timeBar = $this->request->get("timeBar");
        $path = $this->template($content,$is_html,$headBar,$buyList,$timeBar);
        if($path){
            $template = new Template();
            $template->title = $title;
            $template->content = $content;
            $template->path = $path;
            $template->html = $is_html;
            $template->headBar = $headBar;
            $template->buyList = $buyList;
            $template->timeBar = $timeBar;
            $template->update_at = '';
            $template->create_at = date("Y-m-d H:i:s");
            $res = $template->save();
            if(!$res){
                $this->exitMessage(false,"模板保存失败");
            }
            $this->exitMessage(true,"模板保存成功");
        }
    }

    public function editAction(){
        $id = $this->request->get("id");
        $title = $this->request->get("title");
        $content = $this->request->get("content");
        $is_html = $this->request->get("is_html");
        $headBar = $this->request->get("headBar");
        $buyList = $this->request->get("buyList");
        $timeBar = $this->request->get("timeBar");
        if(!$id){
            $this->exitMessage(false,"缺少必要参数");
        }


        $template = Template::findFirst($id);
        $path = $this->template($content,$is_html,$headBar,$buyList,$timeBar,$template->path);
        $template->title = $title;
        $template->content = $content;
        $template->html = $is_html;
        $template->headBar = $headBar;
        $template->buyList = $buyList;
        $template->timeBar = $timeBar;
        $template->path = $path;
        $template->update_at = date("Y-m-d H:i:s");
        $res = $template->save();
        if(!$res){
            $this->exitMessage(false,"编辑模板保存失败");
        }
        $this->exitMessage(true,"编辑模板保存成功");

    }

    public function delAction(){
        $id = $this->request->get("id");
        if(!$id){
            $this->exitMessage(false,"缺少必要参数");
        }
        $template = Template::findFirst($id);
        if(!$template){
            $this->exitMessage(false,"未查询到相关信息");
        }
        $path = $template->path;
        unlink(PUBLIC_PATH."/".$path);
        $template->delete();
        $this->exitMessage(true,"删除成功");
    }

    public function template($content,$is_html,$headBar,$buyList,$timeBar,$path=""){
        $html_start  = '<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>海岛小阿妹</title>
  <link rel="stylesheet" href="../css/statistics.css">
  <script src="../js/zepto.min.js"></script>
  <script src="../js/statistics.js"></script>
</head>

<body>
';
        if(!$is_html){
            $body = '<div class="rich_media_area_primary rich_media_content ">'.$content.'</div>';
        }else{
            $body = $content;
        }
        if($headBar){
            $body.=' <!-- headBar -->
  <div class="header">
    <span class="icon"></span>
    <span class="text">此二维码已通过安全验证，可以放心扫码</span>
    <span class="close"></span>
  </div>
  <script>
    $(function () {
      $(".close").click(function () {
        $(".header").hide();
      });
    })
  </script>';
        }
        if($buyList){
            $body.=' <div class="buy-list">
    <div class="buy-list-wrap">
      <div class="buy-item active-out">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/96.jpg" alt=""> <span class="">何**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item active-before">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/132.jpg" alt=""> <span class="">A**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item active">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/1.jpg" alt=""> <span class="">宝**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/2.jpg" alt=""> <span class="">乐**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/3.jpg" alt=""> <span class="">为**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/4.jpg" alt=""> <span class="">蝶**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/5.jpg" alt=""> <span class="">心**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/6.jpg" alt=""> <span class="">生**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/7.jpg" alt=""> <span class="">孙**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/8.jpg" alt=""> <span class="">小**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/9.jpg" alt=""> <span class="">时**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/10.jpg" alt=""> <span class="">????**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/11.jpg" alt=""> <span class="">园**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/12.jpg" alt=""> <span class="">德**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/13.jpg" alt=""> <span class="">乐**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/14.jpg" alt=""> <span class="">丫**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/15.jpg" alt=""> <span class="">A**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/16.jpg" alt=""> <span class="">贾**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/17.jpg" alt=""> <span class="">櫟**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/18.jpg" alt=""> <span class="">邓**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/19.jpg" alt=""> <span class="">x**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/20.jpg" alt=""> <span class="">燕**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/21.png" alt=""> <span class="">涛**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/22.jpg" alt=""> <span class="">涛**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/23.jpg" alt=""> <span class="">F**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/24.jpg" alt=""> <span class="">꧁**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/25.jpg" alt=""> <span class="">屠**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/26.jpg" alt=""> <span class="">兰**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/2.jpg" alt=""> <span class="">贾**&nbsp;&nbsp;已通过咨询成功下单</span> </div>
      </div>
      <div class="buy-item ">
        <div class="buy-item-wrap"> <img src="http://hsh.liangbaobao.top/hh1/images/20.jpg" alt=""> <span class="">修**&nbsp;&nbsp;已扫描二维码咨询</span> </div>
      </div>
    </div>
  </div>
  <script>
    $(function () {
      setInterval(function () {
        let active_obj = $(".active");
        let before_obj = $(".active-before");
        let out_obj = $(".active-out");
        let i_obj = $(".active + .buy-item");
        if (i_obj.length === 0) {
          $(".buy-item:first-child").addClass(\'active\');
        } else {
          i_obj.addClass(\'active\');
        }
        active_obj.removeClass(\'active\').addClass(\'active-before\');
        before_obj.removeClass(\'active-before\').addClass(\'active-out\');
        out_obj.removeClass(\'active-out\');
      }, 2000);
    })
  </script>';
        }

        if($timeBar){
            $body.='
  <!-- timeBar -->
  <div class="footer">
    <p>优惠剩余时间：<span id="minute"></span>分<span id="second"></span>秒</p>
  </div>
  <script>
    var second = 0;
    $(function () {
      setInterval(function () {
        show_time(false);
      }, 1000);
      show_time(true);
    })

    function show_time(init) {
      if (init === true) {
        second = randNum(300, 480);
      }
      if (second >= 0) {
        let minute_str = "0" + Math.floor(second / 60);
        let second_str = second % 60;
        if (second_str < 10) {
          second_str = "0" + second_str;
        }
        $("#minute").text(minute_str);
        $("#second").text(second_str);
        second--;
      }
    }

    function randNum(min, max) {
      return min + Math.round(Math.random() * (max - min));
    }
  </script>';
        }

        $html_end='</body>
</html>';

        if(!$path){
            $path = 'template/'.time().".html";
        }

        $html = $html_start.$body.$html_end;
        $res = file_put_contents(PUBLIC_PATH."/".$path,$html);
        if(!$res){
            return false;
        }
        return $path;
    }

    public function allAction(){
        $list = Template::find([
            'columns'=>'id,title',
        ]);
        $this->exitMessage(true,'',$list);
    }

}